package reflect;

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class TestReflect {
    public  static void main(String [] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
        Class c=Class.forName("reflect.Student");
        //获取构造方法数组
        Constructor []constructors= c.getDeclaredConstructors();
        for(Constructor con:constructors)
        {
            System.out.println(con);
        }
        System.out.println("___________________________________________________________");
        //获取构造方法
        Constructor constructor=c.getConstructor();
        Object student= constructor.newInstance();
        System.out.println(student);

        Constructor constructor2=c.getDeclaredConstructor(String.class,int.class);
        Object student2= constructor2.newInstance("xcy",3);
        System.out.println(student2);

        System.out.println("___________________________________________________________");

        //获取成员变量
        Field []fields =c.getFields();
        for (Field f:fields)
        {
            System.out.println(f);
        }
        Field field=c.getDeclaredField("age");
        field.set(student,111);
        System.out.println(student);

        Field field2=c.getDeclaredField("name");
        field2.setAccessible(true);
        field2.set(student2,"xxxxx");
        System.out.println(student2);
        System.out.println("___________________________________________________________");

        //获取成员方法
        Method[] methods=c.getDeclaredMethods();
        for (Method method:methods)
        {
            System.out.println(method);

        }
        Method method=c.getMethod("toString");
        Object object= method.invoke(student);
        System.out.println(object);

    }
}
